package com.cennavi.minenavidemo.activity.poi;

import android.os.Bundle;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.Toast;

import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.cennavi.minenavidemo.R;
import com.cennavi.minenavidemo.adapter.InputtipsAdapter;
import com.cennavi.minenavidemo.presenter.PoiPresenter;
import com.minedata.minenavi.mapdal.PoiItem;
import com.minedata.minenavi.poiquery.DistrictItem;
import com.minedata.minenavi.poiquery.RegeocodeAddress;
import com.minedata.minenavi.poiquery.Tip;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.Unbinder;

/**
 * 智能提示搜索
 */
public class PoiInputtipsActivity extends AppCompatActivity implements PoiPresenter.OnPoiSearchListener {

    private InputtipsAdapter inputtipAdapter;
    private List<Tip> lists = new ArrayList<>();
    private PoiPresenter mPoiPresenter;

    private Unbinder mUnbinder;
    /**
     * 搜索
     */
    @BindView(R.id.selectEditText)
    EditText selectEditText;
    /**
     * 删除
     */
    @BindView(R.id.deleteImageView)
    ImageView deleteImageView;
    private RecyclerView rv;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_poiinputtips);
        mUnbinder = ButterKnife.bind(this);

        init();
        initTitleBar();
        initRecyclerView();
    }

    private void init() {
        mPoiPresenter = PoiPresenter.getInstance();
        mPoiPresenter.init(PoiInputtipsActivity.this);
        mPoiPresenter.setOnPoiSearchListener(this);
        mPoiPresenter.initInputtips();
    }

    private void initTitleBar() {

        selectEditText.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {

            }

            @Override
            public void afterTextChanged(Editable s) {
                if (!TextUtils.isEmpty(s.toString())) {
                    deleteImageView.setVisibility(View.VISIBLE);
                    String key = selectEditText.getText().toString().trim();
                    onSearch(key);

                } else {
                    deleteImageView.setVisibility(View.GONE);
                }
            }
        });

        deleteImageView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                selectEditText.setText(" ");
                lists.clear();
                inputtipAdapter.notifyDataSetChanged();
            }
        });
    }

    private void initRecyclerView() {
        rv = findViewById(R.id.rv);

        rv.setLayoutManager(new LinearLayoutManager(this));
        inputtipAdapter = new InputtipsAdapter(this, lists);
        inputtipAdapter.setListener(new InputtipsAdapter.PoiSelectListener() {
            @Override
            public void onPoiSelect(Tip info) {
                showToast(info);
            }
        });
        rv.setAdapter(inputtipAdapter);

    }

    private void showToast(Tip info) {
        Toast.makeText(this, info.toString(), Toast.LENGTH_LONG).show();
    }

    private void onSearch(String key) {
        mPoiPresenter.startInputtips(key);
    }

    @Override
    protected void onStart() {
        super.onStart();
    }

    @Override
    protected void onStop() {
        super.onStop();
    }

    @Override
    protected void onResume() {
        super.onResume();
    }

    @Override
    protected void onPause() {
        super.onPause();
    }

    @Override
    protected void onDestroy() {
        mUnbinder.unbind();
        super.onDestroy();
    }

    @Override
    public void onSearchComplete(ArrayList<PoiItem> poiItems, boolean isSuccess) {

    }

    @Override
    public void onIdSearch(PoiItem poiItem, boolean isSuccess) {

    }

    @Override
    public void onRegeocodeSearched(RegeocodeAddress regeocodeAddress) {

    }

    @Override
    public void onDistrictSearch(DistrictItem districtItem) {

    }

    @Override
    public void onGetInputtips(List<Tip> tips) {
        //处理sug检索结果
        this.lists.clear();

        if (tips != null && tips.size() > 0) {
            lists.addAll(tips);
        }

        inputtipAdapter.notifyDataSetChanged();
    }
}
